<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    // 登录显示
    public function show()
    {
        return view('login.login');
    }

    //登录逻辑
    public function login(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email|min:3',
            'password' => 'required|min:4'
        ]);
        $user = Auth::attempt(['email' => $request->email, 'password' => $request->password]);
        $token = User::where('email', $request->email)->pluck('active');
        if (!$token[0]) {
            session()->flash('danger', '用户没有进行邮箱验证！');
            return redirect()->route('loginshow');
        }
        if ($user && $token[0]) {
            session()->flash('success', '登录成功！');
            return redirect()->route('home');
        }
        session()->flash('danger', '用户名或者密码错误！');
        return redirect()->route('loginshow');
    }

    //退出登录
    public function logout()
    {
        Auth::logout();
        session()->flash('success', "退出登录成功");
        return redirect()->route('loginshow');
    }
}
